# -*- coding: utf-8 -*- 
"""
========================================================================================================================
@project : my-sanic
@file: main
@Author: mengying
@email: 652044581@qq.com
@date: 2023/3/3 10:04
@desc: 项目主入口启动文件
========================================================================================================================
"""
import os
from sanic import Sanic
from config import config
from utils.myCache import Cache
from addict import Dict
from sanic_cors import CORS

# 初始化项目实例
app = Sanic(config.PROJECT_NAME)

# 默认设置可以跨域
CORS(app)

# 配置redis缓存组件, 前两项固定配置
if hasattr(config, 'REDIS_HOST'):
    cacheConfig = Dict()
    cacheConfig.serializer = "ujson"
    cacheConfig.mode = "sync"
    cacheConfig.default.engine = 'memory'
    cacheConfig.redis.host = config.REDIS_HOST
    cacheConfig.redis.port = config.REDIS_PORT
    cacheConfig.redis.db = config.REDIS_DB
    cacheConfig.redis.password = config.REDIS_PASSWORD
    app.ctx.cache = Cache(cacheConfig.to_dict())

# 配置引入中间件
from middleware import myMiddleware

# 配置引入服务监听器
from middleware import myListener

# 配置引入蓝图url
from api import urls
app.blueprint(urls.routers)

if __name__ == '__main__':
    import platform
    if platform.system().lower() == "windows":
        app.run(host='127.0.0.1', port=8085, debug=True, auto_reload=True, access_log=True)
    else:
        app.run(host='0.0.0.0', port=8085, workers=os.cpu_count())